package com.cn.news.controller;

import com.cn.common.pojo.ResultModel;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("admin")
public class FileUpload {
    @PostMapping("fileUpload")
    public ResultModel fileUpload(MultipartFile file) {
        String originalFilename=file.getOriginalFilename();	//获取原文件名
        String newFilename=UUID.randomUUID()+"_"+originalFilename;	//使用UUID避免重名

        File filePath=new File("E:\\java项目\\web5\\upload\\"+newFilename);
        try {
            file.transferTo(filePath);
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
        String successFile="..\\..\\upload\\"+newFilename;
       Map<String,String> map = new HashMap<>();
       map.put("src",successFile);
        return ResultModel.getResultModel("",0,1, map);
    }

}
